package com.example.practice.class04;

/**
 * @Date 2022/9/12
 * @Author Jonathan
 * @Description 返回一个数组中子数组最大累加和
 * @link {https://leetcode.cn/problems/maximum-subarray/}
 */
public class Code02_MaxSubArraySum {
    public static void main(String[] args) {

    }


    public static int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int pre = nums[0]; // 上一步dp的值
        int max = nums[0]; // 假设为最大值
        for (int i = 1; i < nums.length; i++) {
            // 当前值
            int cur = Math.max(pre + nums[i], nums[i]);
            max = Math.max(cur, max);
            pre = cur;
        }

        return max;
    }
}
